#include<QtWebEngineWidgets/QWebEngineSettings>
#include<QtWebEngineWidgets/QWebEngineProfile>
#include<QtWidgets/QApplication>
#include<QtCore/QThreadPool>
#include<QTranslator>

#include<QtWidgets/QMessageBox>

#include"extensionsystem/iplugin.h"
#include"extensionsystem/pluginspec.h"
#include"extensionsystem/pluginmanager.h"
#include"xwaterHelper.h"
#include<utils/SettingsManager.h>
#include<QtWidgets/QCheckBox>
#include<QtWidgets/QButtonGroup>
#include<QtWidgets/QLayout>
#include<QtWidgets/QPushButton>
int main(int argc, char* argv[])
{
	QApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
	//QApplication::setAttribute(Qt::AA_UseDesktopOpenGL);
	//QApplication::setAttribute(Qt::AA_UseOpenGLES);
	//QApplication::setAttribute(Qt::AA_UseSoftwareOpenGL);
	//QApplication::setAttribute(Qt::AA_DontCheckOpenGLContextThreadAffinity);
	QApplication xwaterApp(argc, argv);
	QCoreApplication::setApplicationName("xwater应用程序");
	QCoreApplication::setApplicationVersion("1.0");
	QCoreApplication::setOrganizationName("csdn熊猫啊熊猫");
	QGuiApplication::setApplicationDisplayName("xwater测试程序");
	qInstallMessageHandler(messageHandle);
	const QStringList pluginArguments = xwaterApp.arguments();
	xwaterApp.setAttribute(Qt::AA_EnableHighDpiScaling);
	
	xwaterApp.setAttribute(Qt::AA_UseHighDpiPixmaps);
	xwaterApp.setAttribute(Qt::AA_DisableWindowContextHelpButton);
	QWebEngineSettings::defaultSettings()->setAttribute(QWebEngineSettings::PluginsEnabled, true);
#if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
	QWebEngineSettings::defaultSettings()->setAttribute(QWebEngineSettings::DnsPrefetchEnabled, true);
	QWebEngineProfile::defaultProfile()->setUseForGlobalCertificateVerification();
#endif
	QThreadPool::globalInstance()->setMaxThreadCount(32);


	PluginManager pluginManager;
	PluginManager::setPluginIID(QLatin1String("org.xwater-project.plugin"));
	//PluginManager::setGlobalSettings(globalSettings);
	//PluginManager::setSettings(settings);


	//TODO: QTranslator translator;
	QTranslator qtTranslator;
	QStringList uiLanguages = QLocale::system().uiLanguages();
	QString qmFile = QApplication::applicationDirPath() + "/translations/xwaterapp_zh.qm";
	qtTranslator.load(qmFile);
	QApplication::installTranslator(&qtTranslator);
	//ENDDO

	xwaterApp.setDesktopFileName("org.xwater-project.desktop");

	//LOAD
	QStringList pluginPaths;
	pluginPaths << QApplication::applicationDirPath() + "/ClientPlugins";
	PluginManager::setPluginPaths(pluginPaths);//对目录下的所有插件的json内容进行解析，读取规格


	//寻找启动插件
	QVector<PluginSpec*> plugins = PluginManager::plugins();
	PluginSpec* startPlugin = nullptr;

	//寻找配置的启动插件
	QVariant value = SettingsManager::getInstance().getSettingsValue("xwater", "XWaterSettings/StartupPlugin");
	if (value.isNull()) {

		QButtonGroup btnGrp;
		QDialog dialog;
		QVBoxLayout vL(&dialog);
		for (PluginSpec* spec : plugins){
			
			QCheckBox*  cbx=new QCheckBox(spec->name());
			btnGrp.addButton(cbx);
			dialog.layout()->addWidget(cbx);
		}
		QHBoxLayout btnL;
		QPushButton cancelBtn("取消");
		QPushButton okBtn("确定");
		btnL.addStretch();
		btnL.addWidget(&cancelBtn);
		btnL.addWidget(&okBtn);
		vL.addLayout(&btnL);
		dialog.connect(&cancelBtn, &QPushButton::clicked, &dialog ,&QDialog::reject);
		dialog.connect(&okBtn, &QPushButton::clicked, &dialog,&QDialog::accept);
		int ret=dialog.exec();
		if (ret == QDialog::Rejected) {
			return 1;
		}
		else if(ret == QDialog::Accepted) {
			QCheckBox* cbx = dynamic_cast<QCheckBox*>(btnGrp.checkedButton());
			if (cbx) {
				SettingsManager::getInstance().setSettingsValue("xwater", "XWaterSettings/StartupPlugin", cbx->text());
			}
			
		}
	}
	QString startupPluginName= SettingsManager::getInstance().getSettingsValue("xwater", "XWaterSettings/StartupPlugin").toString();


	//寻找启动插件 ISASClient
	for (PluginSpec* spec : plugins) {
		if (spec->name() == startupPluginName) {
			startPlugin = spec;
			break;
		}
	}

	if (!startPlugin) {
		QString nativePaths = QDir::toNativeSeparators(pluginPaths.join(QLatin1Char(',')));
		const QString title = "发生错误";
		const QString reason = QCoreApplication::translate("Application", "Could not find %1 plugin in %2").arg(startupPluginName).arg(nativePaths);
		QMessageBox::critical(nullptr, title, reason);
		return 1;
	}
	if (!startPlugin->isEffectivelyEnabled()) {
		const QString title ="发生错误";
		const QString reason = QCoreApplication::translate("Application", "%1 plugin is disabled.").arg(startupPluginName);
		QMessageBox::critical(nullptr, title, reason);
		return 1;
	}
	if (startPlugin->hasError()) {
		const QString title = "发生错误";
		QMessageBox::critical(nullptr, title, startPlugin->errorString());
		return 1;
	}
	//
	PluginManager::checkForProblematicPlugins();
	//
	PluginManager::loadPlugin(startPlugin);

	if (startPlugin->hasError()) {
		const QString title = QString("%1插件发生错误").arg(startupPluginName);
		QMessageBox::critical(nullptr, title, startPlugin->errorString());
		return 1;
	}
	QObject::connect(&xwaterApp, &QCoreApplication::aboutToQuit, &pluginManager, &PluginManager::shutdown);

	int appReturnCode = xwaterApp.exec();


	return appReturnCode;
}